﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;

namespace PathSearcher
{
    class TimeTable
    {
        SQLiteConnection connection;
    

        public TimeTable()
        {
            this.connection = Database.GetConnection();
        }
        public List<Line> GetLineTime(int stationId, int startTime, String dayType, int waitTime, List<int> linesToUse)
        {
            StringBuilder sb = new StringBuilder();
            for (int i=0;i<linesToUse.Count;i++)
            {
                if (i != 0)
                {
                    sb.Append(",");
                }
                sb.Append((linesToUse[i]).ToString());

            }

            SQLiteCommand cmnd = new SQLiteCommand(connection);
            cmnd.CommandText = "SELECT line_id, time FROM time_table WHERE station_id=@stationId AND line_id IN " +
                                "("+sb.ToString()+") and time>@startTime and time<@endTime and day_type=@dayType ORDER BY time"; //GROUP BY line_id ORDER BY time ASC LIMIT 1";
            cmnd.Parameters.AddWithValue("stationId", stationId);
            //cmnd.Parameters.AddWithValue("lines", sb.ToString());
            cmnd.Parameters.AddWithValue("startTime", startTime.ToString());
            cmnd.Parameters.AddWithValue("endTime", (startTime + waitTime).ToString());
            cmnd.Parameters.AddWithValue("dayType", dayType);

            List<Line> lines = new List<Line>();
            using (cmnd)
            {
                using (SQLiteDataReader reader = cmnd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Line line = new Line();
                        line.LineID = int.Parse(reader["line_id"].ToString());
                        line.Time = int.Parse(reader["time"].ToString());
                        lines.Add(line);
                    }
                }
            }
            if (lines.Count > 0)
                return lines;
            else
                return null;
        }
    }
}
